package com.citcc.web.controller.business;

import java.util.Date;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.citcc.business.bean.CdzRates;
import com.citcc.business.service.CdzRatesService;
import com.citcc.common.annotation.Log;
import com.citcc.common.base.AjaxResult;
import com.citcc.common.enums.BusinessType;
import com.citcc.common.page.TableDataInfo;
import com.citcc.framework.util.ShiroUtils;
import com.citcc.framework.web.base.BaseController;

@Controller
@RequestMapping("/cdzRates")
public class CdzRatesController  extends BaseController {

    private String prefix = "business/cdzRates";

    @Autowired
    private CdzRatesService cdzRatesService;

    @RequiresPermissions("business:cdzRates:view")
    @GetMapping("/main")
    public String vehicle(){
        return prefix + "/main";
    }

    @RequiresPermissions("business:cdzRates:list")
    @GetMapping("/list")
    @ResponseBody
    public TableDataInfo list(CdzRates rates){
        startPage();
        if (rates.getOrgId()==null||"".equals(rates.getOrgId())) {
            rates.setOrgId(getSysUser().getDeptId());
        }
        List<CdzRates> list=cdzRatesService.selectAllList(rates);
        return getDataTable(list);
    }

    @GetMapping("/add")
    public String toAdd(ModelMap map)
    {
        return prefix + "/add";
    }

    @RequiresPermissions("business:cdzRates:add")
    @Log(title = "充电费率管理", businessType = BusinessType.INSERT)
    @PostMapping("/addSave")
    @ResponseBody
    public AjaxResult addSave(CdzRates rates)
    {
        if (rates.getOrgId()==null||"".equals(rates.getOrgId())) {
            rates.setOrgId(getSysUser().getDeptId());
        }
        rates.setCreateBy(ShiroUtils.getLoginName());
        rates.setDelFlag("0");
        return toAjax(cdzRatesService.add(rates));
    }

    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id")String id, ModelMap map)
    {
        map.put("rates",cdzRatesService.getCdzRates(id));
        return prefix + "/edit";
    }


    @RequiresPermissions("business:cdzRates:edit")
    @Log(title = "充电费率管理", businessType = BusinessType.UPDATE)
    @PostMapping("/editSave")
    @ResponseBody
    public AjaxResult editSave(CdzRates rates)
    {
        rates.setUpdateBy(ShiroUtils.getLoginName());
        rates.setUpdateTime(new Date());
        return toAjax(cdzRatesService.update(rates));
    }

    @RequiresPermissions("business:cdzRates:remove")
    @Log(title = "充电费率管理", businessType = BusinessType.UPDATE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String id)
    {
        try
        {
            Integer e =cdzRatesService.delete(id);
            return toAjax(e);
        }
        catch (Exception e)
        {
            return error(e.getMessage());
        }
    }

}
